import { createUnit, createText, geadientText } from 'jc-color'
import { LoggerLevel } from './enums'

class Logger {
  private _logType?: string
  private _dateTimeFormat: string
  private _logFilePath?: string
  private _maxLogFileSize: string | number
  /**
   * 
   * @param logFilePath 
   * @param dateTimeFormat 
   * @param maxLogFileSize 
   */
  constructor(logType?: string, dateTimeFormat: string = 'zh', logFilePath?: string, maxLogFileSize: number | string = 100) {
    this._logType = logType
    if(logFilePath){
      this._logFilePath = logFilePath;
    }
    this._dateTimeFormat = dateTimeFormat;
    this._maxLogFileSize = maxLogFileSize;
  }

  public trace(...msgs: any[]) {
    this._log(LoggerLevel.TRACE, ...msgs);
  }

  public debug(...msgs: any[]) {
    this._log(LoggerLevel.DEBUG, msgs);
  }

  public info(...msgs: any[]) {
    this._log(LoggerLevel.INFO, msgs);
  }

  public gradient (...msgs: any[]) {
    this._log(LoggerLevel.GRADIENT, msgs);
  }

  public success(...msgs: any[]) {
    this._log(LoggerLevel.SUCCESS, msgs);
  }

  public warn(...msgs: any[]) {
    this._log(LoggerLevel.WARN, msgs);
  }

  public error(...msgs: any[]) {
    this._log(LoggerLevel.ERROR, msgs);
  }

  public fatal(...msgs: any[]) {
    this._log(LoggerLevel.FATAL, msgs);
  }

  public level1(...msgs: any[]) {
    this._log(LoggerLevel.LEVEL1, msgs);
  }

  public level2(...msgs: any[]) {
    this._log(LoggerLevel.LEVEL2, msgs);
  }

  public level3(...msgs: any[]) {
    this._log(LoggerLevel.LEVEL3, msgs);
  }

  private _log(level: LoggerLevel, ...msgs: any[]) {
    let logType = ''
    if(this._logType){
      logType = `[${this._logType}] `
    }
    const _msgToStr = (...msgs:any):string=> {
      return msgs.join(' ')
    }
    const colotTemplate = `${logType}[${new Date().toLocaleString(this._dateTimeFormat)}] [${level}]`
    const fatalTemplate = `${logType}[${new Date().toLocaleString(this._dateTimeFormat)}] [FATAL] `
    const dateTemplate = `${logType}[${new Date().toLocaleString(this._dateTimeFormat)}] ${_msgToStr(...msgs)}`
    let colorUnit;
    switch (level) {
      case LoggerLevel.TRACE:
        colorUnit=createUnit(colotTemplate,"gray");
        break;
      case LoggerLevel.DEBUG:
        colorUnit=createUnit(colotTemplate,"#2AA3FF");
        break;
      case LoggerLevel.INFO:
        colorUnit=createUnit(colotTemplate,"#ffffff");
        break;
      case LoggerLevel.GRADIENT:
        colorUnit=geadientText(dateTemplate,["#84FC2C","yellow"],["red", "blue","#EB6461","green"]);
        break;
      case LoggerLevel.SUCCESS:
        colorUnit=createUnit(colotTemplate,"green");
        break;
      case LoggerLevel.WARN:
        colorUnit=createUnit(colotTemplate,"yellow");
        break;
      case LoggerLevel.ERROR:
        colorUnit=createUnit(colotTemplate,"red");
        break;
      case LoggerLevel.FATAL:
        colorUnit=createUnit(colotTemplate,"#A320B5");
        break;
      case LoggerLevel.LEVEL1:
        colorUnit=createUnit(fatalTemplate+'[LEVEL 1]',"#A320B5").underline();
        break;
      case LoggerLevel.LEVEL2:
        colorUnit=createUnit(fatalTemplate+'[LEVEL 2]',"red").underline()
        colorUnit.bgColor="yellow"
        break;
      case LoggerLevel.LEVEL3:
        colorUnit=createUnit(fatalTemplate+'[LEVEL 3]',"yellow");
        colorUnit.bgColor="red"
        break;
      default:
        colorUnit=createUnit(colotTemplate,"gray");
        break;
    }
    if(level !== LoggerLevel.GRADIENT){
      createText(
        colorUnit,` - ${_msgToStr(...msgs)}\n`
      ).print()
    }else{
      colorUnit.print()
    }
    
  }
}

export {
  LoggerLevel,
  Logger
}